package org.zfes.snowier.cloud.zbss.cms.topic.service.impl;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import org.zfes.snowier.core.data.DataSet;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.beans.factory.annotation.Autowired;
import org.zfes.snowier.core.util.ZBeanUtil;
import org.zfes.snowier.core.util.ZDateUtil;
import org.zfes.snowier.core.util.ZAlert;
import org.zfes.snowier.core.util.ZAssert;
import org.zfes.snowier.common.dao.params.ParamMap;
import org.zfes.snowier.cloud.zbss.cms.topic.service.ICmsTopicAttachmentService;
import org.zfes.snowier.cloud.zbss.cms.topic.service.ICmsTopicColumnService;
import org.zfes.snowier.cloud.zbss.cms.topic.service.ICmsTopicContentService;
import org.zfes.snowier.cloud.zbss.cms.topic.service.ICmsTopicModelService;
import org.zfes.snowier.cloud.zbss.cms.topic.service.ICmsTopicService;
import org.zfes.snowier.cloud.zbss.cms.topic.service.ICmsTopicTxtService;
import org.zfes.snowier.cloud.zbss.cms.topic.dao.CmsTopicMapper;
import org.zfes.snowier.cloud.zbss.cms.topic.model.CmsTopic;
import org.zfes.snowier.cloud.zbss.cms.topic.model.CmsTopicColumn;
import org.zfes.snowier.cloud.zbss.cms.topic.model.CmsTopicModel;

import java.util.stream.Collectors;
import java.util.stream.Stream;
@Service
public class CmsTopicServiceImpl implements ICmsTopicService{
 	@Autowired
	private CmsTopicMapper cmsTopicMapper;
 	private ICmsTopicAttachmentService iCmsTopicAttachmentService;
 	private ICmsTopicColumnService iCmsTopicColumnService;
 	private ICmsTopicContentService iCmsTopicContentService;
 	private ICmsTopicModelService iCmsTopicModleService;
 	private ICmsTopicTxtService iCmsTopicTxtService;
 	private CmsTopicColumn cmsTopicColumn;
 	private CmsTopicModel cmsTopicModle;
	@Transactional(rollbackFor=Exception.class)
 	@Override
	public void createCmsTopic(CmsTopic cmsTopic,String columnIdSet,Long modleId) {
		ZBeanUtil.validateBean(cmsTopic);
		cmsTopic.setStatus(Byte.valueOf("1"));
		cmsTopic.setCreateTime(ZDateUtil.newDate());
		cmsTopic.setUpdateTime(ZDateUtil.newDate());
		cmsTopicMapper.insertSelective(cmsTopic);
		//topic_column表插入记录
		List<Long> columnList=Arrays.asList(columnIdSet.split(",")).stream().map(s->Long.parseLong(s.trim())).collect(Collectors.toList());
		for(int i=0;i<columnList.size();i++) {
			cmsTopicColumn.setTopicId(cmsTopic.getId());
			iCmsTopicColumnService.createCmsTopicColumn(cmsTopicColumn);
		}
		//topic_modle表插入记录
		cmsTopicModle.setTopicId(cmsTopic.getId());
		iCmsTopicModleService.createCmsTopicModel(cmsTopicModle);
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public Optional<CmsTopic> loadCmsTopicById(Long id) {
		if(id!=null){
			return Optional.ofNullable(cmsTopicMapper.selectByPrimaryKey(id));
		}
		return Optional.empty();
		
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void updateCmsTopic(CmsTopic cmsTopic) {
		ZBeanUtil.validateBean(cmsTopic);
		cmsTopic.setUpdateTime(ZDateUtil.newDate());
		cmsTopicMapper.updateByPrimaryKeySelective(cmsTopic);
	}
	
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void deleteCmsTopic(Long id) {
		ZAssert.notNull(id, "请选择要操作的数据");
		iCmsTopicAttachmentService.deleteCmsTopicAttachment(id);
		iCmsTopicColumnService.deleteCmsTopicColumn(id);
		iCmsTopicContentService.deleteCmsTopicContent(id);
		iCmsTopicModleService.deleteCmsTopicModel(id);
		iCmsTopicTxtService.deleteCmsTopicTxt(id);
		cmsTopicMapper.updateStatus(id, Byte.valueOf("-1"));
	}
	@Transactional(rollbackFor=Exception.class)
	@Override
	public void setCmsTopicStatus(Long id, Byte status) {
		ZAssert.notNull(id, "请选择要操作的数据");
		ZAssert.notNull(status, "状态参数错误");
		if(!Stream.of(new Byte[] {-1,0,1}).anyMatch(val->val.equals(status))) {
			ZAlert.serviceLogicalError("状态参数错误");
		}
		cmsTopicMapper.updateStatus( id,  status) ;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public DataSet loadCmsTopicDataSet(Map<String, Object> params) {
		ParamMap pm=ParamMap.filterParam(params);
		pm.getObj("name").ifPresent(v->pm.updateParam("name", "%"+v+"%"));
		DataSet ds=DataSet.newDS2(cmsTopicMapper.selectListCount(pm), cmsTopicMapper.selectMapListPage(pm));
		return ds;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<CmsTopic> loadBySiteId(Long siteId) {
		ZAssert.notNull(siteId, "请选择要操作的数据");
		 List<CmsTopic> cmsTopicList=cmsTopicMapper.selectBySiteId(siteId);
		return cmsTopicList;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<CmsTopic> loaByParentId(Long parentId) {
		ZAssert.notNull(parentId, "请选择要操作的数据");
		List<CmsTopic> cmsTopicList=cmsTopicMapper.selectByParentId(parentId);
		return cmsTopicList;
	}
	@Transactional(rollbackFor=Exception.class,readOnly=true)
	@Override
	public List<CmsTopic> loadByParentIdAndSiteId(Long parentId, Long siteId) {
		ZAssert.notNull(parentId, "请选择要操作的数据");
		ZAssert.notNull(siteId, "请选择要操作的数据");
		List<CmsTopic> cmsTopicList=cmsTopicMapper.selectByParentIdAndSiteId(parentId, siteId);
		return cmsTopicList;
	}
	
}